# -*- coding:utf8 -*-
# !/usr/bin/python
'''
author: ning.luo
createTime: 2022/1/23 22:42
update: ning.luo
updateTime: 2022/1/23 22:42
description: 文件简介描述
'''
import json
import requests

class surveyManagement:

    def __init__(self):
        """
        初始化登录，获取token
        """
        self.base_url = "https://people-survey.staging.shopee.com"
        self.headers = {
            "Content-Type":"application/json",
            "cookie":"PS-Survey=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjozOTEyNTM4OTY0NTE5MDY5OTksImV4cCI6MTY0MzE2NTM2OCwiaXNzIjoiUFMtU3VydmV5IiwibmJmIjoxNjQyOTkxNTY4fQ.V37l9cWo0FOZTPsRaA02pbTORFVN3MQurJOxOysNJDs"
            }

    # def system_admin(self):
        # """
        # 系统登录获取token
        # :return:
        # """
        # url = "/survey/api/v1/external/getToken"
        # headers = {"Content-Type":"application/json"}
        # body = {
        #     "app_id":"ning.luo@shopee.com",
        #     "app_secret":"20220117@Work"
        # }
        # response = requests.post(self.base_url+url, data=json.dumps(body), headers=headers)
        # print(response.text)
        # assert 200 == response.status_code
        # res_json  = response.json()
        # assert res_json["code"] == 200
        # return res_json["data"]


    def create_survey(self, survey_status, **kwargs):
        """
        创建问卷
        :return:
        """
        survey_id = kwargs.get("survey_id", "")


        url = "/survey/api/v1/web/questionnaire/createSurvey"
        blank_survey = {
            "survey_type":2,
            "survey_title":"",
            "survey_note":"",
            "survey_status":0,
            "directory_id":"391273947372510647",
            "question_list":[
                {
                    "question_id":"1643007910941",
                    "question_label":"",
                    "question_value":"",
                    "question_note":"",
                    "question_type":1,
                    "is_required":0,
                    "seq_num":1,
                    "question_options_list":[
                        {
                            "question_options_id":"",
                            "options_type":0,
                            "options_label":"",
                            "options_value":"",
                            "seq_num":1,
                            "is_other":0,
                            "text_entry":0
                        }
                    ]
                }
            ],
            "start_time":0,
            "end_time":0,
            "config":{
                "submit_limit":1,
                "show_description":0,
                "description":"",
                "text":"",
                "show_response":0
            }
        }
        new_survey = {
            "survey_id":survey_id,
            "survey_type":1,
            "survey_title":"问卷调查1",
            "survey_note":"&lt;p&gt;问卷标题描述&lt;/p&gt;",
            "survey_status":5,
            "directory_id":"391273947372510647",
            "question_list":[
                {
                    "question_id":"391717973053774458",
                    "question_label":"",
                    "question_value":"&lt;p&gt;你喜欢足球吗&lt;/p&gt;",
                    "question_note":"",
                    "question_type":1,
                    "is_required":1,
                    "seq_num":1,
                    "question_options_list":[
                        {
                            "question_options_id":"391717973053839994",
                            "options_type":0,
                            "options_label":"",
                            "options_value":"喜欢",
                            "seq_num":1,
                            "img_url":"",
                            "is_other":0,
                            "text_entry":0
                        }
                    ]
                }
            ],
            "survey_access_url":"",
            "start_time":0,
            "end_time":0,
            "survey_version":1,
            "template_type":0,
            "config":{
                "submit_limit":1,
                "show_description":1,
                "description":"",
                "text":"",
                "show_response":1
            }
        }
        if survey_status == 0:
            body = blank_survey
        else:
            body = new_survey
        response = requests.post(self.base_url+url, data=json.dumps(body), headers=self.headers)
        print("创建问卷：", response.text)
        assert 200 == response.status_code
        return response.json()

    def preview_survey(self, survey_id):
        """
        预览问卷
        :return:
        """
        url = "/survey/api/v1/web/questionnaire/previewSurvey"
        body = {
            "survey_id":survey_id,
            "channel_type":0
        }
        response = requests.post(self.base_url+url, data=json.dumps(body), headers=self.headers)
        print("预览问卷：", response.text)
        print("状态码：", response.status_code)
        assert 200 == response.status_code

    def update_survey_status(self, survey_id, survey_status):
        """
        修改问卷状态默认关闭问卷
         5 已发布  8 暂停  10 结束失效 更新方向 【草稿->已发布<->暂停->停止】
        """
        url = "/survey/api/v1/web/questionnaire/updateSurveyStatus"
        body = {
            "survey_id": survey_id,
            "survey_status": survey_status
        }
        print("开始发送请求")
        response = requests.post(self.base_url+url, data=json.dumps(body), headers=self.headers)
        print("问卷状态变更：", response.text)
        assert 200 == response.status_code and response.json()["msg"] == "success","修改问卷状态失败"


    def update_survey(self, survey_id, **kwargs):
        """
        修改问卷
        :return:
        """
        question_id = kwargs.get("question_id", "")
        question_option_id_list = kwargs.get("question_option_id_list", "")

        url = "/survey/api/v1/web/questionnaire/updateSurvey"
        body = {
            "survey_id":survey_id,
            "survey_type":1,
            "survey_title":"问卷调查1--更新测试",
            "survey_note":"&lt;p&gt;问卷标题描述--更新测试&lt;/p&gt;",
            "survey_status":5,
            "directory_id":"391273947372510647",
            "question_list":[
                {
                    "question_id":question_id,
                    "question_label":"",
                    "question_value":"&lt;p&gt;你喜欢足球吗--更新测试&lt;/p&gt;",
                    "question_note":"",
                    "question_type":1,
                    "is_required":1,
                    "seq_num":1,
                    "question_options_list":[
                        {
                            "question_options_id":question_option_id_list[0],
                            "options_type":0,
                            "options_label":"",
                            "options_value":"喜欢--更新测试",
                            "seq_num":1,
                            "img_url":"",
                            "is_other":0,
                            "text_entry":0
                        }
                    ]
                }
            ],
            "survey_access_url":"https://survey.staging.workatsea.com/survey/"+survey_id,
            "start_time":0,
            "end_time":0,
            "survey_version":2,
            "template_type":0,
            "config":{
                "submit_limit":1,
                "show_description":1,
                "description":"",
                "text":"",
                "show_response":1
            }
        }
        response = requests.post(self.base_url+url, data=json.dumps(body), headers=self.headers)
        print("问卷更新结果：", response.text)
        assert 200 == response.status_code

    def delete_survey(self, survey_id):
        """
        删除问卷
        :return:
        """
        url = "/survey/api/v1/web/questionnaire/delete"
        body = {
            "survey_ids":[
                survey_id
            ]
        }
        response = requests.post(self.base_url + url, data=json.dumps(body), headers=self.headers)
        print("问卷删除结果：", response.text)
        assert 200 == response.status_code


if __name__ == "__main__":
    sm = surveyManagement()
    sm.create_survey(0)

    # sm.preview_survey()

    # sm.update_survey()

    # sm.delete_survey()